import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;


public class Edit {

	public JFrame frame;
	private JTextField fname;
	private JTextField lname;
	private JTextField addr;
	private JLabel lblDateOfBirth;
	private JLabel lblDateOfDeath;
	private JLabel lblAddress;
	private JTextField bio;
	private JLabel lblBio;
	private JTextField gender;
	private JLabel lblGender;
	private JButton submit;
	private JLabel wrongname;
	private JLabel genderError;
	private JComboBox monthBirth;
	private JComboBox dayBirth;
	private JComboBox yearBirth;
	private JComboBox monthDeath;
	private JComboBox dayDeath;
	private JComboBox yearDeath;
	private JLabel DateError;
	
	String id;
	Person old;
	private JButton btnCancel;
	private JButton btnEditRelations;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Edit window = new Edit();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Edit() {
		initialize();
		id = MainScreen.personId;
		id = MainScreen.personId;
		frame.setTitle(MainScreen.personSelected);
		populate();
	}
	
	public void populate(){
		Database db = new Database();
		old = db.getPerson(id);
		
		fname.setText(old.fname);
		lname.setText(old.lname);
		
		String dob = old.dob;
		if(!dob.equals("NA")){
			String delims = "[-]+";
			String[] res = dob.split(delims);
			if(res[1].equals("01")){
				monthBirth.setSelectedItem("January");
			}else if(res[1].equals("02")){
				monthBirth.setSelectedItem("February");
			}else if(res[1].equals("03")){
				monthBirth.setSelectedItem("March");
			}else if(res[1].equals("04")){
				monthBirth.setSelectedItem("April");
			}else if(res[1].equals("05")){
				monthBirth.setSelectedItem("May");
			}else if(res[1].equals("06")){
				monthBirth.setSelectedItem("June");
			}else if(res[1].equals("07")){
				monthBirth.setSelectedItem("July");
			}else if(res[1].equals("08")){
				monthBirth.setSelectedItem("August");
			}else if(res[1].equals("09")){
				monthBirth.setSelectedItem("September");
			}else if(res[1].equals("10")){
				monthBirth.setSelectedItem("October");
			}else if(res[1].equals("11")){
				monthBirth.setSelectedItem("November");
			}else if(res[1].equals("12")){
				monthBirth.setSelectedItem("December");
			}
			dayBirth.setSelectedItem(res[0]);
			yearBirth.setSelectedItem(res[2]);
		}else{
			monthBirth.setSelectedItem("January");
			dayBirth.setSelectedItem("01");
			yearBirth.setSelectedItem("1900");
		}
		
		String dod = old.dod;
		if(!dod.equals("NA")){
			String delims = "[-]+";
			String[] res = dod.split(delims);
			if(res[1].equals("01")){
				monthDeath.setSelectedItem("January");
			}else if(res[1].equals("02")){
				monthDeath.setSelectedItem("February");
			}else if(res[1].equals("03")){
				monthDeath.setSelectedItem("March");
			}else if(res[1].equals("04")){
				monthDeath.setSelectedItem("April");
			}else if(res[1].equals("05")){
				monthDeath.setSelectedItem("May");
			}else if(res[1].equals("06")){
				monthDeath.setSelectedItem("June");
			}else if(res[1].equals("07")){
				monthDeath.setSelectedItem("July");
			}else if(res[1].equals("08")){
				monthDeath.setSelectedItem("August");
			}else if(res[1].equals("09")){
				monthDeath.setSelectedItem("September");
			}else if(res[1].equals("10")){
				monthDeath.setSelectedItem("October");
			}else if(res[1].equals("11")){
				monthDeath.setSelectedItem("November");
			}else if(res[1].equals("12")){
				monthDeath.setSelectedItem("December");
			}
			dayDeath.setSelectedItem(res[0]);
			yearDeath.setSelectedItem(res[2]);
		}else{
			monthDeath.setSelectedItem("NA");
			dayDeath.setSelectedItem("NA");
			yearDeath.setSelectedItem("NA");
		}
		
		addr.setText(old.addr);
		bio.setText(old.bio);
		gender.setText(old.sex);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 443);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		fname = new JTextField();
		fname.setBounds(10, 34, 86, 20);
		frame.getContentPane().add(fname);
		fname.setColumns(10);
		
		lname = new JTextField();
		lname.setBounds(10, 78, 86, 20);
		frame.getContentPane().add(lname);
		lname.setColumns(10);
		
		addr = new JTextField();
		addr.setBounds(10, 223, 398, 20);
		frame.getContentPane().add(addr);
		addr.setColumns(10);
		
		JLabel lblNameOfPerson = new JLabel("First Name");
		lblNameOfPerson.setBounds(10, 11, 86, 14);
		frame.getContentPane().add(lblNameOfPerson);
		
		JLabel lblLastName = new JLabel("Last name ");
		lblLastName.setBounds(10, 65, 75, 14);
		frame.getContentPane().add(lblLastName);
		
		lblDateOfBirth = new JLabel("Date of Birth");
		lblDateOfBirth.setBounds(10, 109, 86, 14);
		frame.getContentPane().add(lblDateOfBirth);
		
		lblDateOfDeath = new JLabel("Date of Death");
		lblDateOfDeath.setBounds(10, 153, 86, 14);
		frame.getContentPane().add(lblDateOfDeath);
		
		lblAddress = new JLabel("Address");
		lblAddress.setBounds(10, 202, 66, 14);
		frame.getContentPane().add(lblAddress);
		
		bio = new JTextField();
		bio.setBounds(10, 279, 398, 20);
		frame.getContentPane().add(bio);
		bio.setColumns(10);
		
		lblBio = new JLabel("Bio");
		lblBio.setBounds(10, 254, 46, 14);
		frame.getContentPane().add(lblBio);
		
		gender = new JTextField();
		gender.setBounds(10, 334, 31, 20);
		frame.getContentPane().add(gender);
		gender.setColumns(10);
		
		lblGender = new JLabel("Gender");
		lblGender.setBounds(10, 309, 46, 14);
		frame.getContentPane().add(lblGender);
		
		wrongname = new JLabel("");
		wrongname.setBounds(116, 37, 144, 14);
		frame.getContentPane().add(wrongname);
		
		submit = new JButton("Submit");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Will need to apply NA to all that did not apply
				
				boolean success = true;
                //gets the month in integer form
				String monBirth = dateConvert(monthBirth.getSelectedItem().toString());
				String monDeath = dateConvert(monthDeath.getSelectedItem().toString());
				
				//parse the dates into a string ... integer form
				String dob = dayBirth.getSelectedItem().toString()+"-"+monBirth+"-"+yearBirth.getSelectedItem().toString();
				
				String dod = "";
				if(!monthDeath.getSelectedItem().equals("NA") && !dayDeath.getSelectedItem().equals("NA") && !yearDeath.getSelectedItem().equals("NA")){
					dod = dayDeath.getSelectedItem().toString()+"-"+monDeath+"-"+yearDeath.getSelectedItem().toString();
				}else{
					dod = "NA";
				}
				
				//case statment for fname not empty and gender only one letter m or f 
				if(fname.getText().equals("")){
					success = false;
					wrongname.setText("Cannot be Empty!");
				}else if(gender.getText().equals("M") | gender.getText().equals("F")){
					
				}else{
					success = false;
					genderError.setText("Gender is M or F (Capital)");
				}
					
				if(success){
					wrongname.setText("");
					genderError.setText("");
					DateError.setText("");
					Person p = new Person();
					p.id = id;
					p.fname = fname.getText();
					if(lname.getText().equals("")){
						p.lname = "NA";
					}else{
						p.lname = lname.getText();
					}
					p.dob = dob;
					p.dod = dod;
					p.sex = gender.getText();
					if(addr.getText().equals("")){
						p.addr = "NA";
					}else{
						p.addr = addr.getText();
					}
					if(bio.getText().equals("")){
						p.bio = "NA";
					}else{
						p.bio = bio.getText();
					}
					String desc = "";
					if(old.fname.equals(p.fname) && old.lname.equals(p.lname) && old.dob.equals(p.dob) && old.dod.equals(p.dod) && old.sex.equals(p.sex) && old.addr.equals(p.addr) && old.bio.equals(p.bio)){
						desc = "Nothing";
					}else{
						desc += "Changed ";
						int count = 0;
						if(!old.fname.equals(p.fname)){
							desc += "fname to " + p.fname;
							count++;
						}
						if(!old.lname.equals(p.lname)){
							if(count > 0){
								desc += ", ";
							}
							desc += "lname to " + p.lname;
							count++;
						}
						if(!old.dob.equals(p.dob)){
							if(count > 0){
								desc += ", ";
							}
							desc += "dob to " + p.dob;
							count++;
						}
						if(!old.dod.equals(p.dod)){
							if(count > 0){
								desc += ", ";
							}
							desc += "dod to " + p.dod;
							count++;
						}
						if(!old.sex.equals(p.sex)){
							if(count > 0){
								desc += ", ";
							}
							desc += "gender to " + p.sex;
							count++;
						}
						if(!old.addr.equals(p.addr)){
							if(count > 0){
								desc += ", ";
							}
							desc += "addr to " + p.addr;
							count++;
						}
						if(!old.bio.equals(p.bio)){
							if(count > 0){
								desc += ", ";
							}
							desc += "bio to " + p.bio;
							count++;
						}
					}
					addPerson(p, desc);
				}
			}

			

			
		});
		submit.setBounds(335, 374, 89, 23);
		frame.getContentPane().add(submit);
		
		genderError = new JLabel("");
		genderError.setBounds(51, 334, 346, 14);
		frame.getContentPane().add(genderError);
		
		
		String [] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
		String [] monthsD = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", "NA"};
	    monthBirth = new JComboBox(months);
		monthBirth.setBounds(10, 122, 86, 20);
		frame.getContentPane().add(monthBirth);
		monthBirth.setSelectedIndex(1);
		
		/*
		int size=31;
		if(monthBirth.getSelectedItem().equals("January") | monthBirth.getSelectedItem().equals("March") | monthBirth.getSelectedItem().equals("May") | monthBirth.getSelectedItem().equals("July") | monthBirth.getSelectedItem().equals("September") | monthBirth.getSelectedItem().equals("November")){
			size = 31;
		}else if(monthBirth.getSelectedItem().equals("April") | monthBirth.getSelectedItem().equals("June") | monthBirth.getSelectedItem().equals("August") | monthBirth.getSelectedItem().equals("October") | monthBirth.getSelectedItem().equals("December")){
			size = 30;
		}else{
			size = 29;
		}
			
		int [] num = null;
		for(int i =1; i<size + 1; i++){
			num[i] = i;
		}
		*/
	    String [] num = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
	    String [] numD = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "NA"};
		dayBirth = new JComboBox(num);
		dayBirth.setBounds(116, 122, 46, 20);
		frame.getContentPane().add(dayBirth);
		
		String[] year = new String [315]; 
		int k = 0;
		for(int i = 2013; i>1699; i--, k++){
			String str = i + "";
			year[k] = str;
		}
		String[] yearD = new String [316]; 
		k = 0;
		for(int i = 2013; i>1699; i--, k++){
			String str = i + "";
			yearD[k] = str;
		}
		yearD[315] = "NA";
		
		//System.out.println(year[4]);
			
		
		
		yearBirth = new JComboBox(year);
		yearBirth.setBounds(194, 122, 66, 20);
		frame.getContentPane().add(yearBirth);
		
		monthDeath = new JComboBox(monthsD);
		monthDeath.setSelectedIndex(1);
		monthDeath.setBounds(10, 171, 86, 20);
		frame.getContentPane().add(monthDeath);
		
		dayDeath = new JComboBox(numD);
		dayDeath.setBounds(116, 172, 46, 20);
		frame.getContentPane().add(dayDeath);
		
		yearDeath = new JComboBox(yearD);
		yearDeath.setBounds(194, 172, 66, 20);
		frame.getContentPane().add(yearDeath);
		
	    DateError = new JLabel("");
		DateError.setBounds(136, 153, 219, 14);
		frame.getContentPane().add(DateError);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		btnCancel.setBounds(108, 374, 89, 23);
		frame.getContentPane().add(btnCancel);
		
		btnEditRelations = new JButton("Edit Relations");
		btnEditRelations.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Relations r = new Relations();
				r.frame.setVisible(true);
			}
		});
		btnEditRelations.setBounds(207, 374, 118, 23);
		frame.getContentPane().add(btnEditRelations);
		
		
		
		
	}
	
	private void addPerson(Person p, String desc) {
		Database db = new Database();
		boolean result = db.updatePerson(p);
		if(result){
			boolean edit = db.updateEdits(MainScreen.theUser, desc, id);
			frame.setVisible(false);
		}
	}
	
	private String dateConvert(String string) {
		String str = "";
		if(string.equals("January")){
			str = "01";
	    }else if(string.equals("February")){
	    	str = "02";
	    }else if(string.equals("March")){
	    	str = "03";
	    }else if(string.equals("April")){
	    	str = "04";
	    }else if(string.equals("May")){
	    	str = "05";
	    }else if(string.equals("June")){
	    	str = "06";
	    }else if(string.equals("July")){
	    	str = "07";
	    }else if(string.equals("August")){
	    	str = "08";
	    }else if(string.equals("September")){
	    	str = "09";
	    }else if(string.equals("October")){
	    	str = "10";
	    }else if(string.equals("November")){
	    	str = "11";
	    }else if(string.equals("Decemeber")){
	    	str = "12";
	    }
		return str;
	}
}
